package main

import (
	"fmt"
	"strings"
	"third_part/redis/redisclient/client"
)

const (
	RedisHost = "192.168.100.203"
	RedisPort = 6379
)

type ObjStatus struct {
	sysStatus string
	devStatus string
}

var (
	RedisPool  = redisclient.RedisPool{}
	dev2Status = make(map[string]ObjStatus, 0)

	sysstatusinfo = make(map[string]string, 0)
	devstatusinfo = make(map[string]string, 0)
)

func GetObjStatus() {
	var (
		alarmcount int64
		objId      string
	)
	defer func() {
		fmt.Println("c")
		if err := recover(); err != nil {
			fmt.Println("dump error:", err)
		}
	}()
	RedisPool.InitRedisClient(RedisHost, RedisPort)

	c := RedisPool.GetRedisClient(0)
	allsysstatus, err := c.Keys("sysstatus:*")
	if err != nil {
		fmt.Println("operator [keys(\"sysstatus\")] err:reason:", err.Error())
	}
	alldevstatus, err := c.Keys("devstatus:*")
	if err != nil {
		fmt.Println("operator [keys(\"devstatus\")]", " err:reason:", err.Error())
	}
	for k, v := range allsysstatus {
		fmt.Println("allsysstatus index:", k, "objectId:", v)
	}
	for k, v := range alldevstatus {
		fmt.Println("alldevstatus index:", k, "objectId:", v)
	}
	for _, v := range allsysstatus {
		devKey := v
		sysstatusinfo[strings.TrimSpace(strings.Split(v, ":")[1])], err = c.Get(v)
		if err != nil {
			fmt.Println("operator [Get(\"", devKey, "\")] err:reason:", err.Error())
		}
		objId = strings.TrimSpace(strings.Split(v, ":")[1])
		fmt.Println("sysstatusinfo:", sysstatusinfo)
		dev2Status[objId] = ObjStatus{
			sysStatus: strings.TrimRight(strings.TrimLeft(strings.TrimSpace(strings.Split(sysstatusinfo, ":")[2]), "["), "]"),
		}
	}

	for _, v := range alldevstatus {
		objId := strings.TrimSpace(strings.Split(v, ":")[1])
		sysstatusinfo[objId], err = c.Get(v)
		if err != nil {
			fmt.Println("objId:", objId, " operator [Get(\"", v, "\")] errreason:", err.Error())
		}
		objId = strings.TrimSpace(strings.Split(v, ":")[1])
		fmt.Println("devstatusinfo:", devstatusinfo)
		ObjStatus := dev2Status[objId]
		delete(dev2Status, objId)
		ObjStatus.devStatus = strings.TrimRight(strings.TrimLeft(strings.TrimSpace(strings.Split(devstatusinfo, ":")[3]), "["), "]")
		dev2Status[objId] = ObjStatus
	}
	//check obj status
	for kk, vv := range dev2Status {
		fmt.Println("devStatus:", vv.devStatus, "sysStatus:", vv.sysStatus)
		if strings.Count(vv.sysStatus, "0") != 8 || strings.Count(vv.devStatus, "0") != 8 {
			alarmcount++
			fmt.Println("objId:", kk, "status:alarm")
		}
	}
}

func main() {
	GetObjStatus()
}
